<?php
Class SafePDO extends PDO {

	public static function exception_handler($exception) {
		// Output the exception details
		die('Uncaught exception: '.$exception->getMessage());
	}

	public function __construct($dsn, $username='', $password='', $driver_options=array()) {

		// Temporarily change the PHP exception handler while we . . .
		set_exception_handler(array(__CLASS__, 'exception_handler'));

		// . . . create a PDO object
		parent::__construct($dsn, $username, $password, $driver_options);

		// Change the exception handler back to whatever it was before
		restore_exception_handler();
	}
	
	public function getAllUsers(){
		$ret = array();
		$stmt = $this->prepare("SELECT id_user, name, pass, login FROM `users_bliss`");
		if($stmt->execute()) {
			while ($row = $stmt->fetch()) {
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllIncident(){
		$ret = array();
		$stmt = $this->prepare("SELECT id_incident, machine, type, complement, date_detection, date_resolution FROM `incident_bliss`");
		if($stmt->execute()){
			while ($row = $stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllSalles(){
		$ret=array();
		$stmt=$this->prepare("SELECT id_salle, nom_salle FROM `salle_bliss`");
		if($stmt->execute()){
			while ($row=$stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllType_Incident(){
		$ret = array();
		$stmt = $this->prepare("SELECT id_type_incident, nom_type FROM `type_incident_bliss`");
		if($stmt->execute()){
			while($row = $stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllMachine(){
		$ret = array();
		$stmt = $this->prepare("SELECT id_machine, IP, nom, salle FROM `machine_bliss`");
		if($stmt->execute()){
			while($row = $stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}

}

define('PDO_DSN','mysql:dbname=bliss;host=localhost');
define('PDO_USER','bliss_user');
define('PDO_PASSWORD','root');

// Connect to the database with defined constants
$dbh = new SafePDO(PDO_DSN, PDO_USER, PDO_PASSWORD);